package com.jike.mahjong.common.weixin;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;

/**
 * Created by hengxing on 2017/1/23.
 */
public class WeixinUtil {


    /**
     * 微信支付
     * @param orderId 订单编号
     * @param actualPay 实际支付金额
     * @return
     */
    private String generateOrderInfoByWeiXinPay(String orderId, float actualPay, HttpServletRequest request, HttpServletResponse response) throws Exception {
         /* String notify_url = propertiesService.WEI_XIN_NOTIFY_URL;//回调地址
        String uuid = IdGen.uuid();
        System.out.print("uuid" + uuid);
        SortedMap<String, String> signParams = new TreeMap<String, String>();
        signParams.put("appid", ConstantUtil.APP_ID);//app_id
        signParams.put("body", "测试");//商品参数信息
        signParams.put("mch_id", ConstantUtil.PARTNER);//微信商户账号
        signParams.put("nonce_str", uuid);//32位不重复的编号
        signParams.put("notify_url", notify_url);//回调页面
        signParams.put("out_trade_no", orderId);//订单编号
        signParams.put("spbill_create_ip", request.getRemoteAddr());//请求的实际ip地址
        signParams.put("total_fee", "1");//支付金额 单位为分
        signParams.put("trade_type", "APP");//  付款类型为APP
        String sign = PayCommonUtil.createSign("UTF-8", signParams);//生成签名
        signParams.put("sign", sign);
        signParams.remove("key");//调用统一下单无需key（商户应用密钥）
        String requestXml = PayCommonUtil.getRequestXml(signParams);//生成Xml格式的字符串
        String result = CommonUtil.httpsRequest(ConstantUtil.UNIFIED_ORDER_URL, "POST", requestXml);//以post请求的方式调用统一下单接口
      //  （注：ConstantUtil.UNIFIED_ORDER_URL = https://api.mch.weixin.qq.com/pay/unifiedorder;    ）返回的result成功结果取出prepay_id：
        Map map = XMLUtil.doXMLParse(result);
        String return_code = (String) map.get("return_code");
        String prepay_id = null;
        String returnSign = null;
        String returnNonce_str = null;
        if (return_code.contains("SUCCESS")) {
            prepay_id = (String) map.get("prepay_id");//获取到prepay_id
        }
        StringBuffer weiXinVo = new StringBuffer();
        long currentTimeMillis = System.currentTimeMillis();//生成时间戳
        long second = currentTimeMillis / 1000L;//（转换成秒）
        String seconds = String.valueOf(second).substring(0, 10);//（截取前10位）
        SortedMap<String, String> signParam = new TreeMap<String, String>();
        signParam.put("appid", ConstantUtil.APP_ID);//app_id
        signParam.put("partnerid", ConstantUtil.PARTNER);//微信商户账号
        signParam.put("prepayid", prepay_id);//预付订单id
        signParam.put("package", "Sign=WXPay");//默认sign=WXPay
        signParam.put("noncestr", uuid);//自定义不重复的长度不长于32位
        signParam.put("timestamp", seconds);//北京时间时间戳
        String signAgain = PayCommonUtil.createSign("UTF-8", signParam);//再次生成签名
        signParams.put("sign", signAgain);
        weiXinVo.append("appid=").append(ConstantUtil.APP_ID).append("&partnerid=").append(ConstantUtil.PARTNER).append("&prepayid=").append(prepay_id).append("&package=Sign=WXPay").append("&noncestr=").append(uuid).append("&timestamp=").append(seconds).append("&sign=").append(signAgain);//拼接参数返回给移动端
        return weiXinVo.toString();*/
        return  null;
    }

}
